package com.act.web;

import com.act.domain.Employee;
import com.act.dto.PagerDTO;
import com.act.dto.Response;
import com.act.service.api.IEmployeeService;
import com.act.util.FileUtil;
import com.act.util.LoggerUtil;
import com.act.vo.PagerVo;

import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/employee")
public class EmployeeController {
	@Resource
	private IEmployeeService employeeService;
		
    private final Logger LOGGER = Logger.getLogger(EmployeeController.class);

    /**
     *	使用springmvc进行文件上传处理
     */
    @RequestMapping(value = "/loadphoto", method = RequestMethod.POST)
        public Response add(HttpServletRequest request,@RequestParam CommonsMultipartFile file) {
        String[] names = request.getParameterValues("name");
        FileUtil.uploadFile(file,names[0]);
        return Response.success();
    }


    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public Response add(Employee employee
//    		,@RequestParam("file") MultipartFile file
    		) {
    	 try {
//    		 if(!file.isEmpty()) {
//    			 file.transferTo(new File("D:/objcet/pictures/"+file.getOriginalFilename()));
//    		 }
    		 employeeService.add(employee);
             return Response.success();
         } catch (Exception e) {
        	 LoggerUtil.error(LOGGER, e);
             return Response.fail();
         }
    }
    
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public Response update(Employee employee) {
    	 try {
    		 employeeService.update(employee);
             return Response.success();
         } catch (Exception e) {
        	 LoggerUtil.error(LOGGER, e);
             return Response.fail();
         }
    }

    @RequestMapping(value = "/updatePWD",method = RequestMethod.POST)
    @ResponseBody
    public Response updatePWD(Employee employee) {
    	 try {
    		 employeeService.updatePWD(employee);
             return Response.success();
         } catch (Exception e) {
        	 LoggerUtil.error(LOGGER, e);
             return Response.fail();
         }
    }
    
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.GET)
    @ResponseBody
    public Response delete(@PathVariable int id) {
    	 try {
    		 employeeService.delete(id);
             return Response.success();
         } catch (Exception e) {
        	 LoggerUtil.error(LOGGER, e);
             return Response.fail();
         }
    }

    @RequestMapping(value="/get/{status}/{jobnum}/{name}",method=RequestMethod.GET)
    @ResponseBody
    public Response getAllByEmpidOrName(@PathVariable int status,@PathVariable String jobnum,@PathVariable String name) throws UnsupportedEncodingException{
        try {
            List<Employee> list=employeeService.getEmployeesByEmployeeIdOrName(status,jobnum, name);
            return Response.success(list);
        }catch (Exception e) {
            LoggerUtil.error(LOGGER, e);
        }
        return Response.fail();        
    }
    
    
    @RequestMapping(value="/login/{jobnum}/{password}",method=RequestMethod.GET)
    @ResponseBody
    public Response login(@PathVariable String jobnum,@PathVariable String password,
    		HttpSession session){
        try {
            Employee emp=employeeService.login(jobnum, password);
            if(emp!=null) {
            	session.setAttribute("employee", emp);
            	return Response.success(emp);
            }
        }catch (Exception e) {
            LoggerUtil.error(LOGGER, e);
        }
        return Response.fail();
    }

    //查询所有员工
    @RequestMapping(value="/get",method = RequestMethod.POST)
    @ResponseBody
    public Response getAll(@RequestBody PagerDTO pagerDTO){
  		try {
  			PagerVo<Map> amlist=employeeService.getAll(pagerDTO);

  			return Response.success(amlist);
  		} catch (Exception e) {
  			LoggerUtil.error(LOGGER, e);
  		}
  		return Response.fail();
    }
    
    //根据部门id查询该部门所有员工
    @RequestMapping(value="/getByDepartId/{departId}",method = RequestMethod.POST)
    @ResponseBody
    public Response getAll(@PathVariable int departId,@RequestBody PagerDTO pagerDTO){
  		try {
  			PagerVo<Map> amlist=employeeService.getByDepartId(pagerDTO, departId);

  			return Response.success(amlist);
  		} catch (Exception e) {
  			LoggerUtil.error(LOGGER, e);
  		}
  		return Response.fail();
    }
    
	//根据员工id查询该员工信息
    @RequestMapping(value="/getById/{id}",method=RequestMethod.GET)
    @ResponseBody
    public Response  getEmployeeById(@PathVariable int id){
  		try {
  			Employee employee=employeeService.getEmployeeById(id);

  			return Response.success(employee);
  		} catch (Exception e) {
  			LoggerUtil.error(LOGGER, e);
  		}
  		return Response.fail();
    }
    
	//根据员工id查询该员工信息
    @RequestMapping(value="/getByStatus/{status}",method=RequestMethod.POST)
    @ResponseBody
    public Response  getByStatus(@PathVariable int status,@RequestBody PagerDTO pagerDTO){
  		try {
  			PagerVo<Map> amlist =employeeService.getByStatus(pagerDTO, status);

  			return Response.success(amlist);
  		} catch (Exception e) {
  			LoggerUtil.error(LOGGER, e);
  		}
  		return Response.fail();
    }
    
	//根据员工id查询该员工信息
    @RequestMapping(value="/getAll",method=RequestMethod.GET)
    @ResponseBody
    public Response  getAllList(){
  		try {
  			List<Employee> list =employeeService.getAllList();

  			return Response.success(list);
  		} catch (Exception e) {
  			LoggerUtil.error(LOGGER, e);
  		}
  		return Response.fail();
    }
}
